package cn.blunce.ListSort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Experiment on 2016/5/1 0001.
 */
public class ListSort {
    static class User {
        private String name;
        private String time;

        User(String name, String time) {
            this.name = name;
            this.time = time;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", time='" + time + '\'' +
                    '}';
        }
    }

    public static void test_list_sort_by_time() {
        List<User> users = new ArrayList<User>();
        users.add(new User("b", "19901030 11:30:02"));
        users.add(new User("a", "19871028 11:30:01"));
        users.add(new User("c", "19901030 11:32:02"));

        for (User user : users)
            System.out.println(user.toString());

        System.out.println("\n");

        //from big one to small one
        Collections.sort(users, new Comparator<User>() {
            public int compare(User o1, User o2) {
                return -(o1.getTime().compareTo(o2.getTime()));
            }
        });

        for (User user : users)
            System.out.println(user.toString());
    }

}
